package com.xl6667.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.dto.ExcelCategoryDto;
import com.xl6667.domain.entity.Category;
import com.xl6667.enums.AppHttpCodeEnum;
import com.xl6667.service.CategoryService;
import com.xl6667.util.BeanCopyUtil;
import com.xl6667.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("content/category")
public class SysCategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        return categoryService.listAllCategory();
    }

    /**
     * 获取所有分类
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */

    @GetMapping("/list")
    public ResponseResult listCategory(Integer pageNum,Integer pageSize, String name,String status){
        return categoryService.listCategory(pageNum,pageSize,name,status);
    }

    /**
     * 添加分类
     * @param category
     * @return
     */

    @PostMapping
    public ResponseResult addCategory(@RequestBody Category category){
        if (category.getPid()==null){
            category.setPid(-1L);
        }
        return categoryService.addCategory(category);
    }

    /**
     * 删除分类
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    public ResponseResult delCategory(@PathVariable List<Long> ids){
        return categoryService.delCAtegory(ids);
    }

    /**
     * 分类回显
     */
    @GetMapping("{id}")
    public ResponseResult getCategory(@PathVariable Long id){
        return categoryService.getCategory(id);
    }
    /**
     * 修改分类
     */
    @PutMapping
    public  ResponseResult putCategory(@RequestBody Category category){
        return categoryService.putCategory(category);
    }
    @PutMapping("/changeStatus")
    public ResponseResult changeStatusCategory(@RequestBody Category category){
        return categoryService.changeStatusCategory(category);
    }
    /**
     * 分类导出
     */
    @GetMapping("/export")
    public void categoryExport(HttpServletResponse response){
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        try {
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            List<Category> list = categoryService.list();
            List<ExcelCategoryDto> excelCategoryDtos = BeanCopyUtil.copyBeanList(list, ExcelCategoryDto.class);
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ExcelCategoryDto.class)
                    .autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryDtos);

        } catch (Exception e) {

            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(), "下载文件失败" );
            WebUtils.renderString(response, JSON.toJSONString(result));

        }
    }
}
